#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
struct telephone{
 	char name[20];
 	char phonenumber[20];
 	telephone *next;//定义一个指向结构类型的指针
}*head,//链表头
*current,*ptr;//current表示当前链表，ptr是当前链表的前一个链表
void enter();
void del();
void list();
void search();
void save();
void load();

int main(){
	int mode;
	do{
		printf("Choose your mode:\n\n");
		printf("0-退出通讯录\n");
		printf("1-通讯录录入\n");
		printf("2-通讯录查询\n");
		printf("3-通讯录删除\n");
		printf("4-通讯录显示\n");
		printf("5-通讯录保存\n");
		printf("6-通讯录加载\n");
		scanf("%d",&mode);
		system("cls");
		switch(mode){
			case 0:printf("已退出");
				break;
			case 1:enter();
				break;
			case 2:search();
				break;
			case 3:del();
				break;
			case 4:list();
				break;
			case 5:save();
				break;
			case 6:load();
				break;
		}
	}while(mode != 0);
 	return 0;
 }
 
void enter(){//通讯录录入函数
 	fflush(stdin);//清空输入区缓存
//	char input[20];
	int k=1;//跳出循环变量
	//输入联系人
	while(k == 1){
		current = (telephone*)malloc(sizeof(telephone));//为current开辟内存空间
		if (head == NULL){//如果是头节点
		 	head = current;//令头节点为当前节点
		}
		else{
			ptr->next = current;//令前一节点指向当前节点
		}
		current->next = NULL;
		//输入人名与电话号码
		printf("请输入联系人姓名:\n");
		scanf("%s",current->name);
		printf("请输入电话号码:\n");
		scanf("%s",current->phonenumber);
		
		ptr = current;//当前节点为下一个前一节点
		printf("输入'0'返回菜单,输入'1'继续添加\n");//控制是否跳出循环
		scanf("%d",&k);
		if(k == 0){
			return;//跳出
		}
	}


		
	system("cls");//清屏
	printf("已结束\n");
		
 }
 
 void del(){//通讯录删除函数
 	list();//展示通讯录内容
 	current = head;//从头节点开始
 	int choise;//类型选择变量
	char input[20];
 	printf("按姓名删除，输入1，按电话号码删除，输入0:");//按不同的类型搜索
 	scanf("%d",&choise);
 	if(choise){//按姓名删除
 		printf("输入名称：\n");
 		scanf("%s",&input);//输入目标名字
 		while(current->next != NULL&&strcmp(current->name,input) != 0){//循环查找要删除的节点，找到后跳出循环
 			ptr = current;
 			current = current->next;
		};
		if(strcmp(current->name,input) == 0){
			printf("已删除%s",current->name);
 			if(strcmp(head->name,input) == 0){//找到的节点是头节点
 				head = current->next;
			}
			else{//找到的节点是普通节点
				ptr->next = current->next;
			}
			free(current);//释放当前节点内存

		}
		else{//没有找到要删除的节点
			printf("未找到");
		}
	}
	else if(!choise){//按电话号码删除
		printf("输入号码：\n");
 		scanf("%s",&input);//输入目标号码
 		while(current->next != NULL&&strcmp(current->phonenumber,input) != 0){//循环查找目标节点，找到后跳出循环
 			ptr = current;
 			current = current->next;
		};
		if(strcmp(current->phonenumber,input) == 0){
			printf("已删除%s",current->name);
 			if(strcmp(head->phonenumber,input) == 0){//找到的节点是头节点
 				head = current->next;
			}
			else{//找到的节点是普通节点
				ptr->next = current->next;
			}
			free(current);

		}
		else{//没有找到目标节点
			printf("未找到");
		}
	}
	else{
		printf("输入错误！");
	}
 }
 
 void list(){//通讯录列表显示
 	current = head;//从头节点开始
	while (current != NULL){
		printf("%s:%s\n",current->name,current->phonenumber);//输出人名与学号
		current = current->next;
	};
	printf("显示完成\n");
 }
 
 void search(){//通讯录查询函数
 	current = head;//从头节点开始
 	int choise;//类型选择变量
	char input[20];
 	printf("按姓名查找，输入1，按电话号码查找，输入0:");
 	scanf("%d",&choise);//模式选择
 	if(choise){//按名称搜索
 		printf("输入名称：\n");
 		scanf("%s",&input);//输入名称
 		while(current->next != NULL&&strcmp(current->name,input) != 0){//循环查找要删除的节点
 			current = current->next;
		};
		if(strcmp(current->name,input) == 0){//比较是否为当前节点
			printf("已找到\n");
			printf("%s:%s",current->name,current->phonenumber);
		}
		else{//没有找到要删除的节点
			printf("未找到\n");
		}
	}
	else if(!choise){//按号码搜索
		printf("输入号码：\n");
 		scanf("%s",&input);
 		while(current->next != NULL&&strcmp(current->phonenumber,input) != 0){//循环查找目标节点，找到后跳出循环
 			current = current->next;
		};
		if(strcmp(current->phonenumber,input) == 0){//比较是否为当前节点
			printf("已找到\n");
			printf("%s:%s",current->name,current->phonenumber);
		}
		else{//没有找到目标节点
			printf("未找到\n");
		}
	}
	else{
		printf("输入错误！\n");
	}
 }
 
 void save(){//通讯录保存函数
 	current = head;
	FILE *fp=fopen("book.txt","w");//打开文件，写入
	while (current != NULL){
		fprintf(fp,"%s %s\n",current->name,current->phonenumber);//将链表写入到文件中
		current = current->next;
	}
//	清除指针current的内存
	current = head;
	while (current != NULL){
	 	free(current);
		current = current->next;
	}
	fclose(fp);//关闭文件
	system("cls");//清屏
	printf("已保存\n");
 }
 
 void load(){//通讯录加载函数
 	fflush(stdin);//清空输入缓存
	FILE *fp=fopen("book.txt","r");//打开文件，只读
//	current = (telephone*)malloc(sizeof(telephone));
//	fscanf(fp,"%s %s",current->name,current->phonenumber);
//	head = current;
	while(1){
		current = (telephone*)malloc(sizeof(telephone));//为当前节点开辟缓存空间
		if (head == NULL){
			 	head = current;
		}
		else{
		ptr->next = current;
		}
		current->next = NULL;
		if(EOF == fscanf(fp,"%s %s",current->name,current->phonenumber)){//如果读取未完
			ptr->next = NULL;
			free(current);
			break;
		}
//		printf("%s %s\n",current->name,current->phonenumber);
		ptr = current;
		current = current->next;
	};
	fclose(fp);//关闭文件
	system("cls");//清屏
	printf("读取完成\n");
 }